<?php
/**
 *	KLF Framework
 *
 *	Written by Kristian Oye <kristianoye@gmail.com> on Oct 23, 2011
 *	Copyright (C) 2010-2011
 */
namespace KLF\System\Web
{
	use KLF\System\KComponent;
	use KLF\System\KPropertyContext;
	use KLF\System\Errors\KReadOnlyException;
	use KLF\System\Errors\KArgumentException;
	use KLF\System\Web\SessionState\KSessionHandler;
	use KLF\System\Web\SessionState\KSessionStateBase;
	use KLF\System\Web\SessionState\KSessionStateMode;
	
	/**
	 *  Class KHttpSession
	 *
	 *  @package KLF\System\Web
	 *  @access public
	 *  @author Kristian Oye <kristianoye@gmail.com>
	 *  @since 0.1
	 *  
	 *  @method KHttpSession NewInstance
	 *  
	 *  @property string $CodePage The code page.
	 *  @property array $SessionData The contents of the session.
	 *  @property KSessionStateMode $SessionStateMode
	 *  @property boolean $IsCookieless True if session is not presisted with cookies.
	 *  @property boolean $IsNewSession
	 */
	class KHttpSession extends KComponent implements \ArrayAccess
	{
		/**
		 * Returns true if the session relies on a SESSIONID URL parameter
		 * instead of an HTTP cookie.
		 * 
		 * @var bool
		 */
		protected $_propIsCookieless;
		
		/**
		 * The flag is true if the session is new or unregistered.
		 * 
		 * @var boolean $_rpropIsNewSession
		 */
		protected $_rpropIsNewSession;
		
		/**
		 * The session handler responsible for handling the actual 
		 * session state.
		 * 
		 * @var KSessionStateBase $_rpropSessionHandler
		 */
		protected $_rpropSessionHandler;
		
		/**
		 * Returns the unique session ID.
		 * 
		 * @var string $_rpropSessionID
		 */
		protected $_rpropSessionID;
		
		/**
		 * The type of storage used for sessions.
		 * 
		 * @var KSessionStateMode $_propSessionStateMode
		 */
		protected $_propSessionStateMode;
		
		/**
		 * Indicates how long (in minutes) that the session will time out.
		 * 
		 * @var int $_propTimeout
		 */
		protected $_propTimeout;
		
		/**
		 * Read-only property Keys
		 * 
		 * @param mixed $val
		 * @param int $pc
		 * @throws KReadOnlyException
		 */
		public function propKeys($val=NULL, $pc = 0)
		{
			if ($pc === KPropertyContext::PROP_GET)
			{
				return array_keys($_SESSION);
			}
		}
		
		public function __construct(KHttpApplication $app)
		{
			global $_SESSION;
			
			$this->_propIsCookieless = ($app->Config->GetValue('application.cookieMode', 'UseCookies') === 'UseCookies' ? 0 : 1);
			$this->_propSessionStateMode = KSessionStateMode::InProc();
			$this->_rpropIsNewSession = (strlen(session_id()) === 0);
			$this->_rpropSessionID = session_id();
			$this->_propTimeout = 15;
			
			$config = KSessionStateBase::GetHandler(
				$app->Config->GetValue('SessionProvider', 
					array('Mode' => 'InProc')));
		}
		
		/**
		 * Returns true if the session value is defined, false if not.
		 * 
		 * @param string $offset
		 */
		public function offsetExists($offset)
		{
			global $_SESSION;
			
			return isset($_SESSION[$offset]);
		}
		
		/**
		 * Returns the value if it is defined within the session.
		 * 
		 * @param string $offset
		 */
		public function offsetGet($offset)
		{
			return ($this->offsetExists($offset) ?
				$_SESSION[$offset] : NULL);
		}
		
		/**
		 * Set a session value.
		 * 
		 * @param string $offset
		 * @param mixed $value
		 */
		public function offsetSet($offset, $value)
		{
			global $_SESSION;
			
			if ($this->IsNewSession)
			{
			}
			$_SESSION[$offset] = $value;
		}
		
		/**
		 * Unset a value in the session.
		 * 
		 * @param string $offset
		 */
		public function offsetUnset($offset)
		{
			unset($_SESSION[$offset]);
		}
	}
}
